package com.lm.caffeine.example;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

public class WeightedCacheExample {
    public static void main(String[] args) {
        // 创建缓存实例
        Cache<String, String> cache = Caffeine.newBuilder()
                .maximumWeight(10_000) // 最大权重（例如字节数）
                .weigher((String key, String value) -> value.length()) // 权重计算
                .build();

        // 添加缓存
        cache.put("key1", "smallValue"); // 权重较小
        cache.put("key2", "thisIsALargeValueExceedingWeightLimit"); // 权重大

        // 读取缓存
        System.out.println("key1: " + cache.getIfPresent("key1"));
        System.out.println("key2: " + cache.getIfPresent("key2"));
    }
}
